package croe.jdbc.utils;

import croe.jdbc.dao.RowProcessor;
import croe.jdbc.dao.impl.BasicRowProcessor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * User: zengzhili
 * Time: 2014-06-07 21:27
 */
@SuppressWarnings("unchecked")
public class ResultSetUtils {

    private static RowProcessor basicRowProcessor;

    static {
        basicRowProcessor = new BasicRowProcessor();
    }


    public static Object[] toArray(ResultSet rs) throws SQLException {
        return  basicRowProcessor.toArray(rs);
    }

    public static <T> T toBean(ResultSet rs, Class<T> type) throws SQLException {
        return basicRowProcessor.toBean(rs,type);
    }

    public static <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException {
        return basicRowProcessor.toBeanList(rs,type);
    }


    public static Map<String, Object> toMap(ResultSet rs) throws SQLException {
        return basicRowProcessor.toMap(rs);
    }

    public static List<Map<String, Object>> toListMap(ResultSet rs) throws SQLException {
        List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
        while (rs.next()) {
            Map<String,Object> dataMap = toMap(rs);
            dataList.add(dataMap);
        }
        return dataList;
    }


}
